package com.astrofizzbizz.stoneedgegui.monitor;

import java.util.StringTokenizer;

import com.astrofizzbizz.stoneedgegui.utilities.StoneEdgeIIIException;
import com.astrofizzbizz.stoneedgegui.utilities.StoneEdgeIIITunnel;




public class TelescopeStatus
{
	
	public static String cantOpenReason(StoneEdgeIIITunnel stoneEdgeIIITunnel) throws StoneEdgeIIIException
	{
	    String[] response = {"",""};
		response = stoneEdgeIIITunnel.executeCommand("tx slit", 1024);

		StringTokenizer st = new StringTokenizer(response[0]);
		if (st.hasMoreTokens()) {st.nextToken();} 
		else {throw new StoneEdgeIIIException("no info on tx slit");}
		if (st.hasMoreTokens()) {st.nextToken();} 
		else {throw new StoneEdgeIIIException("no info on tx slit");}
		
		boolean slitClosed = false;
		if (st.nextToken().equals("slit=closed")) slitClosed = true;
		if(slitClosed)
		{
			int indexCant = response[0].indexOf("cantopen");
			if (indexCant < 0) return "Dome Can Open";
			String cantOpenReason = response[0].substring(indexCant + 9);
			if (cantOpenReason == null)
			{
				return "Dome Can Open";
			}
			else
			{
				return cantOpenReason;
			}
		}
		else
		{
			return "Dome Open";
		}
	}
	public static boolean slitClosed(StoneEdgeIIITunnel stoneEdgeIIITunnel) throws StoneEdgeIIIException
	{
	    String[] response = {"",""};
		response = stoneEdgeIIITunnel.executeCommand("tx slit", 1024);
		StringTokenizer st = new StringTokenizer(response[0]);
		if (st.hasMoreTokens()) {st.nextToken();} 
		else {throw new StoneEdgeIIIException("no info on tx slit");}
		if (st.hasMoreTokens()) {st.nextToken();} 
		else {throw new StoneEdgeIIIException("no info on tx slit");}
		
		boolean slitClosed = false;
		if (st.nextToken().equals("slit=closed")) slitClosed = true;
		return slitClosed;
	}
	public static double domeLocation(StoneEdgeIIITunnel stoneEdgeIIITunnel) throws StoneEdgeIIIException
	{
	    String[] response = {"",""};
		response = stoneEdgeIIITunnel.executeCommand("tx dome", 1024);
		StringTokenizer st = new StringTokenizer(response[0]);
		if (st.nextToken().toUpperCase().equals("ERROR")) return -1001.0;
		if (st.hasMoreTokens()) {st.nextToken();} 
		else {throw new StoneEdgeIIIException("no info on tx dome");}
		
		double domeLocation = Double.parseDouble(st.nextToken().substring(3));
		return domeLocation;
	}
	public static boolean trackingOff(StoneEdgeIIITunnel stoneEdgeIIITunnel) throws StoneEdgeIIIException
	{
	    String[] response = {"",""};
		response = stoneEdgeIIITunnel.executeCommand("tx track", 1024);
		StringTokenizer st = new StringTokenizer(response[0]);
		if (st.hasMoreTokens()) {st.nextToken();} 
		else {throw new StoneEdgeIIIException("no info on tx track");}
		if (st.hasMoreTokens()) {st.nextToken();} 
		else {throw new StoneEdgeIIIException("no info on tx track");}
		
		boolean trackingOff = true;
		double track = Double.parseDouble(st.nextToken().substring(3));
		if (track > 10.0) trackingOff = false;
		return trackingOff;
	}
	public static String statusTable(int numRows, StoneEdgeIIITunnel stoneEdgeIIITunnel) throws StoneEdgeIIIException
	{
	    String[] response = {"",""};
		String command = "tail -"+ numRows +" /data/tserver.log";
		response = stoneEdgeIIITunnel.executeCommand(command, numRows * 100);
		int indexEnd = 0;
		int indexBegin = -1;
		String[] table = new String[numRows + 1];
		int icount = 0;
		
		while (indexEnd >= 0)
		{
			indexEnd = response[0].indexOf("\n", indexBegin + 1);
			if (indexEnd > 0) 
			{
				table[icount] = response[0].substring(indexBegin + 1, indexEnd);
				table[icount] = table[icount].replaceFirst("T", "   ");
				table[icount] = table[icount].replaceFirst("Z", "Z  ");
				icount = icount + 1;
				indexBegin = indexEnd;
			}
		}
		String orderedTable = "";
		if (icount > 0)
		{
			orderedTable = table[icount - 1];
			for ( int ii = 1; ii < icount; ++ii)
			{
				orderedTable = orderedTable + "\n" + table[icount - 1 - ii];
			}
			orderedTable = orderedTable + "\n";
		}
		return orderedTable;
	}
	public static void main(String[] args) throws StoneEdgeIIIException  
	{
	    StoneEdgeIIITunnel stoneEdgeIIITunnel = new StoneEdgeIIITunnel(StoneEdgeIIITunnel.privateKey());	
	    
	    try {System.out.println("trackingOff = " + TelescopeStatus.trackingOff(stoneEdgeIIITunnel));} 
	    catch (StoneEdgeIIIException e) {System.out.println("trackingOff = UNKNOWN");}
	    try {System.out.println("domeLocation = " + TelescopeStatus.domeLocation(stoneEdgeIIITunnel));} 
	    catch (StoneEdgeIIIException e) {System.out.println("domeLocation = UNKNOWN");}
	    try {System.out.println("slitClosed = " + TelescopeStatus.slitClosed(stoneEdgeIIITunnel));} 
	    catch (StoneEdgeIIIException e) {System.out.println("slitClosed = UNKNOWN");}
	    try {System.out.println("cantOpenReason = " + TelescopeStatus.cantOpenReason(stoneEdgeIIITunnel));} 
	    catch (StoneEdgeIIIException e) {System.out.println("cantOpenReason = UNKNOWN");}
	    try {System.out.println(TelescopeStatus.statusTable(20, stoneEdgeIIITunnel));} 
	    catch (StoneEdgeIIIException e) {System.out.println("statusTable = UNKNOWN");}
	    stoneEdgeIIITunnel.disconnect();
	    System.exit(0);

	}

}
